<?php
namespace app\patrol\controller;
use app\patrol\logic\PatrolLogic;

/**
 * 巡店基础控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/15
 * Time: 11:10
 */
class Patrol extends Base {

    public function __construct(){
        parent::__construct();
        $this->p_logic = new PatrolLogic();
    }

    /**
     * 获取社区巡店用户
     * @return \think\response\Json
     */
    public function ajax_get_cp_user(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->p_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        $maps = [];
        $maps[] = ['openid','=',$u_detail['openid']];
        $v_detail = model('visitor')->get_detail($maps,'id,headimgurl');
        $u_detail['headimgurl'] = $v_detail['headimgurl'];

        $user_info = [];
        $user_info['username'] = $u_detail['username'];
        $user_info['headimgurl'] = $v_detail['headimgurl'];

        //获取常用巡店分部
        $maps = [];
        $maps[] = ['id','=',$u_detail['use_branchid']];
        $b_detail = model('branch')->get_detail($maps,'id,name');
        $user_info['branchid'] = $b_detail['id'];
        $user_info['branch_zw'] = $b_detail['name'];

        //根据常用巡店分部，获取社区巡店分部信息
        $cp = new \app\common\controller\Cp();
        $cp_branch_info = $cp->get_cp_branch_info($b_detail['id']);
        $user_info['cp_branchid'] = $cp_branch_info['id'];

        $month_time = get_month_time();
        $start_date = date('Y-m',strtotime($month_time['start_time']));

        //获取用户巡店总次数
        $maps = [];
        $maps[] = ['openid','=',$u_detail['openid']];
        $maps[] = ['timeid','=',$start_date];
        $cfum_detail = model('cp_form_user_month')->get_detail($maps,'id,cp_user_num');
        $user_info['cp_user_num'] = $cfum_detail['cp_user_num'];

        //获取分部未巡店次数
        $maps = [];
        $maps[] = ['branchid','=',$b_detail['id']];
        $maps[] = ['timeid','=',$start_date];
        $cfbm_detail = model('cp_form_branch_month')->get_detail($maps,'id,cp_branch_num,cp_branch_no_num');
        $user_info['cp_branch_no_num'] = $cfbm_detail['cp_branch_no_num'];

        $crm = new \app\common\controller\CpRankMonth();

        //计算用户社区巡店排名
        $cp_user_ranking = $crm->count_cp_user_ranking($cfum_detail['cp_user_num']);
        $user_info['cp_user_ranking'] = $cp_user_ranking;

        //计算分部社区巡店排名
        $cp_branch_ranking = $crm->count_cp_branch_ranking($cfbm_detail['cp_branch_num']);
        $user_info['cp_branch_ranking'] = $cp_branch_ranking;

        $data['user_info'] = $user_info;
        return json($this->p_logic->get_response(1,'OK',$data));
    }

    /**
     * 保存社区巡店分部
     * @return \think\response\Json
     */
    public function ajax_save_cp_branch(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->b_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        $u_data = [];
        $u_data['use_branchid'] = $params['branchid'];

        $maps = [];
        $maps[] = ['openid','=',$u_detail['openid']];
        $r = model('user')->force()->save($u_data,$maps);
        if($r !== false){
            return json($this->p_logic->get_response(1,'保存成功'));
        }
        return json($this->p_logic->get_response(0,'保存失败'));
    }

    /**
     * 修改工位
     * @return \think\response\Json
     */
    public function ajax_save_ws(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查修改工位规则
        $r = $this->p_logic->check_save_ws_rule($params);
        if(!$r['code']){
            return json($r);
        }
        $ws_detail = $r['data']['ws_detail'];

        $ws_data = [];
        $ws_data['measure'] = trim($params['measure']);
        //$ws_data['price'] = deal_number_mul_100(trim($params['price']));
        $ws_data['work_type'] = $params['work_type'];
        $ws_data['suggest_person_num'] = trim($params['suggest_person_num']);
        $ws_data['window_direction'] = trim($params['window_direction']);
        $ws_data['is_indoor_air'] = $params['is_indoor_air'];
        $ws_data['ws_pic'] = json_encode($params['ws_pic'],JSON_UNESCAPED_UNICODE);

        $maps = [];
        $maps[] = ['id','=',$ws_detail['id']];
        $r = model('work_station')->force()->save($ws_data,$maps);
        if($r !== false){
            return json($this->p_logic->get_response(1,'修改成功'));
        }
        return json($this->p_logic->get_response(0,'修改失败'));
    }

    /**
     * 修改公司
     * @return \think\response\Json
     */
    public function ajax_save_company(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查修改公司规则
        $r = $this->p_logic->check_save_company_rule($params);
        if(!$r['code']){
            return json($r);
        }
        $ws_detail = $r['data']['ws_detail'];

        $ws_data = [];
        $ws_data['company_name'] = trim($params['company_name']);
        $ws_data['company_person'] = trim($params['company_person']);
        $ws_data['company_tel'] = trim($params['company_tel']);
        $ws_data['company_work_preson'] = json_encode($params['company_work_preson'],JSON_UNESCAPED_UNICODE);
        $ws_data['company_out_time'] = trim($params['company_out_time']);
        $ws_data['company_in_time'] = trim($params['company_in_time']);
        $ws_data['company_introduce'] = trim($params['company_introduce']);
        $ws_data['company_business'] = trim($params['company_business']);
        $ws_data['company_pic'] = json_encode($params['company_pic'],JSON_UNESCAPED_UNICODE);

        $maps = [];
        $maps[] = ['id','=',$ws_detail['id']];
        $r = model('work_station')->force()->save($ws_data,$maps);
        if($r !== false){
            return json($this->p_logic->get_response(1,'修改成功'));
        }
        return json($this->p_logic->get_response(0,'修改失败'));
    }

    /**
     * 获取历次沟通备注
     * @return \think\response\Json
     */
    public function ajax_get_remark(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = $this->p_logic->get_search_remark_maps($params['wsid']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('cp_region_ws')->get_list($maps,'id,openid,remark,c_time','id desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            //查找用户
            $u_maps = [];
            $u_maps[] = ['openid','=',$v['openid']];
            $u_detail = model('user')->get_detail($u_maps,'id,username');
            $list[$k]['username'] = $u_detail['username'];
        }

        $data['list'] = $list;
        $data['page'] = $params['page'];
        return json($this->p_logic->get_response(1,'OK',$data));
    }
}

